package pl.edu.pw.elka.GIS.UI;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;

import com.trolltech.qt.core.QModelIndex;
import com.trolltech.qt.core.Qt.ItemDataRole;
import com.trolltech.qt.gui.QAbstractTableModel;

import pl.edu.pw.elka.GIS.graph.Graph;

public class Schedule extends QAbstractTableModel 
{
	private HashMap<Integer,ArrayList<Integer>> schedule;
	
	public Schedule(Graph graph)
	{
		schedule = graph.getVertexByColor();
		
	}

	@Override
	public int columnCount(QModelIndex parent)
	{
		int maxSize = 0;
		
		for (Entry<Integer, ArrayList<Integer>> vertexForColor : schedule.entrySet())
		{
			if (vertexForColor.getKey() > maxSize)
				maxSize = vertexForColor.getKey();
		}
		
		return maxSize;
	}

	@Override
	public Object data(QModelIndex index, int role)
	{
		if(role == ItemDataRole.DisplayRole)
		{
			Integer column = index.column() + 1;
			ArrayList<Integer> vertexForColor = schedule.get(column);
			
			if(vertexForColor != null)
				if(vertexForColor.size() > index.row())
					return vertexForColor.get(index.row());
		}
			
		return null;
	}

	@Override
	public int rowCount(QModelIndex parent)
	{
		int maxSize = 0;
		
		for (Entry<Integer, ArrayList<Integer>> vertexForColor : schedule.entrySet())
		{
			if (vertexForColor.getValue().size() > maxSize)
				maxSize = vertexForColor.getValue().size();
		}
		return maxSize;
	}
}
